<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php if(count($this->getItemCollection()->getItems()) > 0): ?>
<div class="box-collateral box-up-sell">
    <h2 class="ucsell-header"><?php echo $this->__('check these monster savings') ?></h2>
	<?php 
		//get list of bestseller product
		$_bestsellerProductCollection = $this->helper('gtgobbler')->getBestsellerItems();
	?>  
    <ul class="products-grid" id="upsell-product-grid">
    <?php $this->setColumnCount(3); // uncomment this line if you want to have another number of columns. also can be changed in layout ?>
    <?php $this->resetItemsIterator() ?>   
	<?php for($_j=0;$_j<$this->getColumnCount();$_j++): ?>
    <?php if($_link=$this->getIterableItem()): ?>
    <li class="item <?php if($_j===0) echo 'first';else if($_j===2) echo 'last';?>" id="productContainer_<?php echo $_link->getId()?>">
		<a href="<?php echo $_link->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_link, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_link, 'small_image')->resize(223); ?>" width="223" height="220" alt="<?php echo $this->stripTags($this->getImageLabel($_link, 'small_image'), null, true) ?>" /></a>
		<a class="quickview button" href="javascript:void(0);" id="quickviewBtn-<?php echo $_link->getId()?>"><span>quick view</span></a>
		<?php 
			$isSoldOut = '';
			$isLimitedStock = '';
			$isBestSeller = '';
			$isNew = '';
		?>
		<?php if(!$_link->isSaleable()) $isSoldOut = 'sold-out' ?>			
		<?php 
			$stockModel = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_link);
			$stockQty = $stockModel->getStockQty();			
			if($_link->isSaleable() && $stockQty > 0 && $stockQty < 20)
			  $isLimitedStock = ' limited-stock';      
		?>		
		<?php
			if(in_array($_link->getId(), $_bestsellerProductCollection))
				$isBestSeller = ' best-seller';
			
			if($this->isNewProduct($_link)){
				$isNew = ' new';
			}
		?>		
		<div class="product-status<?php echo $isSoldOut.$isLimitedStock.$isBestSeller.$isNew?>"></div>
		<h2 class="product-name"><a href="<?php echo $_link->getProductUrl() ?>" title="<?php echo $this->stripTags($_link->getName(), null, true) ?>"><?php echo $_link->getName()?></a></h2>  
		<?php echo $this->getPriceHtml($_link, true) ?>
		<div class="actions">
			<?php if($_link->isSaleable()): ?>
			<div class="increase">
				<input type="text" readonly="readonly" id="qty-list-<?php echo $_link->getId()?>" value="1"/>
				<span class="up pointer" onclick="increaseQty(true, false, false, <?php echo $_link->getId()?>, <?php echo $this->helper('gtgobbler/catalog')->getMaxIncreaseQty($_link)?>)">up</span>
				<span class="down pointer" onclick="decreaseQty(true, false, false, <?php echo $_link->getId()?>)">down</span>
			</div>
			<?php if ($_link->getTypeInstance(true)->hasOptions($_link)):?>
				<button style="width: 168px;" id='mc-<?php echo $_link->getId()?>' class="button arrow-down mcbutton"><span><span>more choices</span></span></button>
				<?php $configurableAttributes = $_link->getTypeInstance(true)->getConfigurableAttributesAsArray($_link)?>
				<div class="select-flavour-container-list" id="flavourc-<?php echo $_link->getId()?>">
				<?php foreach($configurableAttributes as $attribute):?>
					<?php if(is_int(strpos($attribute['attribute_code'], 'flavour'))):?>
					<table>
						<col />
						<col width="35"/>
						<tbody>
							<?php $f=1;$count=count($attribute['values']);
							foreach($attribute['values'] as $value):?>
								<?php
									$childProduct = $_link->getTypeInstance(true)->getProductByAttributes(array($attribute['attribute_id'] => $value['value_index']), $_link);
									$childProduct = Mage::getModel('catalog/product')->load($childProduct->getId());
									if(!$childProduct->isSaleable()) 
										continue;
								?>
								<tr <?php if($f == $count) echo 'class="last"'?> onclick="addToCartAjax(<?php echo $_link->getId()?>, true, false, false, <?php echo $attribute['attribute_id']?>, <?php echo $value['value_index']?>)">												
									<td class="label">													
										<?php echo $value['label']?>													
									</td>
									<td class="addto">
										<span>add to</span>
										<span class="big">cart</span>
									</td>												
								</tr>
							<?php $f++?>
							<?php endforeach?>
						</tbody>
					</table>
					<?php endif?>
				<?php endforeach?>
				</div>						
			<?php else:?>
				<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="addToCartAjax(<?php echo $_link->getId()?>, true, false, false)"><span><span><?php echo $this->__('add to cart') ?></span></span></button>
			<?php endif?>
			<?php else: ?>
				<p class="availability out-of-stock"><span><?php echo $this->__('Sold Out') ?></span></p>
			<?php endif; ?>
			<p class="added-notify" id="an-<?php echo $_link->getId()?>"></p>
		</div>      
    </li>
    <?php else: ?>
    
    <?php endif; ?>
	<?php endfor; ?>        
    </ul>
    <script type="text/javascript">    
    function customAddToCart(url, productId){
		var addToLink = url+'qty/'+$('qty-'+productId).value;   
		setLocation(addToLink);
    }   
	</script>
</div>
<?php endif ?>
